/**
 * 
 */
package com.spaceserviver.screens;

import java.util.ArrayList;
import java.util.List;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.spaceserviver.misc.ImageButton;
import com.spaceserviver.misc.ImageButton.ClickListener;
import com.spaceserviver.misc.ImageCheckbox;
import com.spaceserviver.misc.ImageCheckbox.StateChangeListener;

/**
 * @author kot
 * 
 */
public class MainMenu implements Screen {

	/** sprite batch to draw text **/
	private SpriteBatch spriteBatch;

	private Stage stageUi;

	private Screen nextScreen;
	private Boolean isDone = false;

	private ImageButton startGameBtn;
	private ImageCheckbox soundChb;
	private ImageButton instructionBtn;
	private ImageButton scoreBtn;

	private List<Texture> textures;

	public MainMenu(final Application app) {

		textures = new ArrayList<Texture>();

		spriteBatch = new SpriteBatch();
		stageUi = new Stage(app.getGraphics().getWidth(), app.getGraphics()
				.getHeight(), false);

		ClickListener startBtnClickListener = new ClickListener() {
			@Override
			public void onClick(ImageButton button) {
				nextScreen = new GameLoop(app);
				isDone = true;

			}
		};

		ClickListener instructionBtnClickListener = new ClickListener() {
			@Override
			public void onClick(ImageButton button) {
				nextScreen = new Instruction(app);
				isDone = true;

			}
		};

		ClickListener scoreBtnClickListener = new ClickListener() {
			@Override
			public void onClick(ImageButton button) {
				nextScreen = new Statistic(app);
				isDone = true;

			}
		};

		StateChangeListener soundClickListener = new StateChangeListener() {

			@Override
			public void onStateChanged(ImageCheckbox checkbox) {
				// TODO Auto-generated method stub

			}

		};

		startGameBtn = CreateButton("startBtn", startBtnClickListener);
		startGameBtn.x = stageUi.centerX() - startGameBtn.width / 2;
		startGameBtn.y = stageUi.top() - startGameBtn.height - 10;
		stageUi.addActor(startGameBtn);

		soundChb = CreateCheckBox("Sound", soundClickListener);
		soundChb.x = stageUi.centerX() - soundChb.width / 2;
		soundChb.y = (startGameBtn.y - startGameBtn.height) - soundChb.height
				- 1;
		soundChb.SetState(true);
		stageUi.addActor(soundChb);

		instructionBtn = CreateButton("instructionBtn",
				instructionBtnClickListener);
		instructionBtn.x = stageUi.centerX() - instructionBtn.width / 2;
		instructionBtn.y = (soundChb.y - soundChb.height)
				- instructionBtn.height - 1;
		stageUi.addActor(instructionBtn);

		scoreBtn = CreateButton("scoreBtn", scoreBtnClickListener);
		scoreBtn.x = stageUi.centerX() - scoreBtn.width / 2;
		scoreBtn.y = (instructionBtn.y - instructionBtn.height)
				- scoreBtn.height - 1;
		stageUi.addActor(scoreBtn);

		app.getInput().setInputProcessor(stageUi);
	}

	private ImageButton CreateButton(String name, ClickListener clickListener) {

		// LoadTextures
		Texture btnUpTexture = new Texture(Gdx.files.internal("data/btns/"
				+ name + "Up.png"));
		textures.add(btnUpTexture);
		Texture btnDwnTexture = new Texture(Gdx.files.internal("data/btns/"
				+ name + "Dwn.png"));
		textures.add(btnDwnTexture);

		TextureRegion upRegion = new TextureRegion(btnUpTexture, 0, 0, 60, 30);
		TextureRegion dwnRegion = new TextureRegion(btnDwnTexture, 0, 0, 60, 30);

		ImageButton btn = new ImageButton(upRegion, dwnRegion);
		btn.clickListener = clickListener;

		return btn;
	}

	private ImageCheckbox CreateCheckBox(String name,
			StateChangeListener stateListener) {
		// LoadTextures
		Texture btnUpTexture = new Texture(Gdx.files.internal("data/btns/"
				+ name + "Up.png"));
		textures.add(btnUpTexture);
		Texture btnDwnTexture = new Texture(Gdx.files.internal("data/btns/"
				+ name + "Dwn.png"));
		textures.add(btnDwnTexture);

		TextureRegion upRegion = new TextureRegion(btnUpTexture, 0, 0, 60, 30);
		TextureRegion dwnRegion = new TextureRegion(btnDwnTexture, 0, 0, 60, 30);

		ImageCheckbox btn = new ImageCheckbox(upRegion, dwnRegion);
		btn.stateListener = stateListener;

		return btn;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.spaceserviver.screens.Screen#update(com.badlogic.gdx.Application)
	 */
	@Override
	public void update(Application app) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.spaceserviver.screens.Screen#render(com.badlogic.gdx.Application)
	 */
	@Override
	public void render(Application app) {

		Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

		stageUi.draw();

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.spaceserviver.screens.Screen#isDone()
	 */
	@Override
	public boolean isDone() {
		return isDone;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.spaceserviver.screens.Screen#dispose()
	 */
	@Override
	public void dispose() {
		spriteBatch.dispose();
		stageUi.dispose();
		for (Texture texture : textures) {
			texture.dispose();
		}
	}

	@Override
	public Screen getNextScreen() {
		return nextScreen;
	}
}
